import { _decorator, Component, Node, Rect, tween, UIOpacity, UITransform, Vec2 } from 'cc';
import { PinAction } from './PinAction';
import { ElementAction } from './ElementAction';

import { Global } from './Global';
import { DataModel } from './DataModel';
const { ccclass, property } = _decorator;

@ccclass('LayerAction')
export class LayerAction extends Component {
    layer_group_id = 0;
    layer_color: any = null;
    start() {
    }

    update(deltaTime: number) {
    }

    public get_hole_num(): number {
        let hole_num: number = 0;
        this.node.children.forEach(element_node => {
            hole_num += element_node.getComponent(ElementAction).get_hole_num();
        });
        return hole_num;
    }

    public init_layer() {
        this.layer_group_id = DataModel.get_new_group_index();
        this.layer_color = DataModel.get_layer_color();
        //console.log('板子颜色',this.layer_color)

        this.node.children.forEach(element_node => {
            element_node.getComponent(ElementAction).init_element(this.layer_group_id, this.layer_color);
        });
    }

    public init_pin(color_pin_arr: any[]) {
        this.node.children.forEach(element_node => {
            element_node.getComponent(ElementAction).init_pin(this.layer_group_id, color_pin_arr);
        });
    }

    get_pin_color(arr: PinAction[]) {
        this.node.children.forEach(element_node => {
            element_node.getComponent(ElementAction)?.get_pin_color(arr);
        });
    }

    public get_element_size(): number {
        return this.node.children.length;
    }

    public layer_active(active: boolean, t: number = 0.3) {
        let need_show = !this.node.active;
        this.node.active = active;
        // this.node.opa
        let opc_1 = 100;
        let opc_2 = 200;
        if (need_show && active && this.node.getComponent(UIOpacity)) {
            this.node.getComponent(UIOpacity).opacity = 0;
            tween(this.node.getComponent(UIOpacity))
                .to(t, { opacity: opc_1 }, { easing: 'quadInOut' })
                .to(t, { opacity: opc_2 }, { easing: 'quadInOut' })
                .to(t, { opacity: opc_1 }, { easing: 'quadInOut' })
                .to(t, { opacity: opc_2 }, { easing: 'quadInOut' })
                .to(t, { opacity: opc_1 }, { easing: 'quadInOut' })
                .to(t, { opacity: opc_2 }, { easing: 'quadInOut' })
                .to(t, { opacity: opc_1 }, { easing: 'quadInOut' })
                .to(t, { opacity: 255 }, { easing: 'quadInOut' })

                .call(() => {
                    this.node.getComponent(UIOpacity).opacity = 255;
                })
                .start();
        }

        this.show_outline(false);
    }

    /** 显示轮廓 */
    show_outline(b = true) {
        if (b) {
            this.node.active = true;
            this.node.children.forEach(element_node => {
                const ele_act = element_node.getComponent(ElementAction)
                if(ele_act )
                    ele_act.show_outline();
            });
        }
        else {
            this.node.children.forEach(element_node => {
                const ele_act = element_node.getComponent(ElementAction)
                if(ele_act )
                    ele_act.show_node();
            });
        }
    }

}

